//
//  EJSHomeCategoryViewController.m
//  EnjoyShoppingStreet
//
//  Created by lifayu on 2017/12/14.
//  Copyright © 2017年 lifayu. All rights reserved.
//

#import "EJSHomeCategoryViewController.h"
#import "EJSHomeBannerView.h"
#import "EJSHomeCategoryHeaderReusableView.h"
#import "EJSGoodsCCell.h"
#import "EJSCategoryCollectionViewCell.h"
#import "EJSHorizontalGoodsCCell.h"
#import "EJSGoodsListViewController.h"
#import "EJSGoodsDetailInfoViewController.h"

#import "EJSAdRequest.h"
#import "EJSGoodsCategoryRequest.h"
#import "EJSGoodsListRequest.h"

@interface EJSHomeCategoryViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout,SDCycleScrollViewDelegate>{
    NSInteger _currentPage;
}

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) EJSHomeBannerView *bannerView;
@property (strong, nonatomic) NSMutableArray *categoryAdList;
@property (strong, nonatomic) NSMutableArray *hotCategoryList;//热门分类
@property (strong, nonatomic) NSMutableArray *hotGoodsList;//热销单品
@property (strong, nonatomic) NSMutableArray *inSeasonGoodsList;//当季新品
@property (strong, nonatomic) NSMutableArray *categoryGoodsList;//分类商品

@end

@implementation EJSHomeCategoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _currentPage = 1;
    [self getCategoryAdRequest];
    [self getHotCategoryList];
    [self getCategoryGoods: @"1"];
    [self getCategoryGoods: @"2"];
    [self getCategoryGoods];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


- (void)configOwnViews
{
    [self.collectionView registerClass:[EJSHomeCategoryHeaderReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"EJSHomeCategoryHeaderReusableView"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"EJSGoodsCCell" bundle:nil] forCellWithReuseIdentifier:@"EJSGoodsCCell"];
    [self.collectionView registerNib:[UINib nibWithNibName:@"EJSCategoryCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"EJSCategoryCollectionViewCell"];
     [self.collectionView registerNib:[UINib nibWithNibName:@"EJSHorizontalGoodsCCell" bundle:nil] forCellWithReuseIdentifier:@"EJSHorizontalGoodsCCell"];
    
    self.bannerView = [[EJSHomeBannerView alloc] initWithFrame:CGRectMake(0, -k_EJSHomeBannerViewHeight, ScreenWidth, k_EJSHomeBannerViewHeight)];
    self.bannerView.cycleScrollView.delegate = self;
    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _currentPage = 1;
        [self getCategoryAdRequest];
        [self getHotCategoryList];
        [self getCategoryGoods: @"1"];
        [self getCategoryGoods: @"2"];
        [self getCategoryGoods];
    }];
    self.collectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _currentPage ++;
        [self getCategoryGoods];
    }];
    self.collectionView.mj_header.ignoredScrollViewContentInsetTop = 0;
    [self.collectionView addSubview:self.bannerView];
}

- (void)setCategoryAdList:(NSMutableArray *)categoryAdList{
    [self.collectionView.mj_header endRefreshing];
    [self.collectionView.mj_footer endRefreshing];
    if(_categoryAdList != categoryAdList){
        _categoryAdList = categoryAdList;
        NSMutableArray *adImageUrls = [NSMutableArray array];
        for (EJSAdModel *model in _categoryAdList) {
            [adImageUrls addObject:model.img?model.img:@""];
        }
        [self.bannerView.cycleScrollView setImageURLStringsGroup:adImageUrls];
        if(_categoryAdList.count > 0){
            [self.collectionView addSubview:self.bannerView];
            self.collectionView.contentInset = UIEdgeInsetsMake(k_EJSHomeBannerViewHeight, 0, 0, 0);
            self.collectionView.mj_header.ignoredScrollViewContentInsetTop = k_EJSHomeBannerViewHeight;
        }else{
            [self.bannerView removeFromSuperview];
            self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
            self.collectionView.mj_header.ignoredScrollViewContentInsetTop = 0;
        }
    }
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    if(index < self.categoryAdList.count){
        EJSAdModel *model = self.categoryAdList[index];
        if([model.linkType isEqualToString:@"goods"]){
            EJSGoodsDetailInfoViewController *goodsDetailVC = [[EJSGoodsDetailInfoViewController alloc] initWithNibName:@"EJSGoodsDetailInfoViewController" bundle:nil];
            goodsDetailVC.goodsId = model.linkId;
            [self.navigationController pushViewController:goodsDetailVC animated:YES];
        }else if([model.linkType isEqualToString:@"category"]){
            EJSCategoryModel *categoryModel = [EJSCategoryModel new];
            categoryModel.categoryId = model.linkId;
            categoryModel.categoryName = model.adName;
            EJSGoodsListViewController *goodsListVC = [[EJSGoodsListViewController alloc] initWithNibName:@"EJSGoodsListViewController" bundle:nil];
            goodsListVC.categoryModel = categoryModel;
            [self.navigationController pushViewController:goodsListVC animated:YES];
        }
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 4;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if(section == 0){
        return self.hotCategoryList.count;
    }else if (section == 1){
        if(self.hotGoodsList.count > 0){
            return 1;
        }
        return 0;
    }
    else if (section == 2){
        if(self.inSeasonGoodsList.count > 0){
            return 1;
        }
        return 0;
    }else if (section == 3){
        return self.categoryGoodsList.count;
    }
    return 0;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        EJSCategoryCollectionViewCell *ccell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EJSCategoryCollectionViewCell" forIndexPath:indexPath];
        [ccell setCategoryModel:self.hotCategoryList[indexPath.item]];
        return ccell;
    }else if (indexPath.section == 1){
        EJSHorizontalGoodsCCell *horizontalGoodsCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EJSHorizontalGoodsCCell" forIndexPath:indexPath];
        [horizontalGoodsCell setGoodsList:self.hotGoodsList];
        return horizontalGoodsCell;
    }else if (indexPath.section == 2){
        EJSHorizontalGoodsCCell *horizontalGoodsCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EJSHorizontalGoodsCCell" forIndexPath:indexPath];
        [horizontalGoodsCell setGoodsList:self.inSeasonGoodsList];
        return horizontalGoodsCell;
    }else{
        EJSGoodsCCell *goodsCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EJSGoodsCCell" forIndexPath:indexPath];
        [goodsCell setGoodsModel:self.categoryGoodsList[indexPath.item]];
        return goodsCell;
    }
    return [UICollectionViewCell new];
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    EJSHomeCategoryHeaderReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"EJSHomeCategoryHeaderReusableView" forIndexPath:indexPath];
    if(indexPath.section == 0){
        headerView.titleLabel.text = @"= 热门分类 =";
    }else if (indexPath.section == 1){
        headerView.titleLabel.text = @"= 热销单品 =";
    }else if (indexPath.section == 2){
        headerView.titleLabel.text = @"= 当季新品 =";
    }else if (indexPath.section == 3){
        headerView.titleLabel.text = @"= 推荐商品 =";
    }
    return headerView;
}
#pragma mark - UICollectionViewDelegateFlowLayout

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if(section == 0){
        if(self.hotCategoryList.count > 0){
            return UIEdgeInsetsMake(0, 15, 0, 15);
        }
    }else if (section == 3){
        if(self.categoryGoodsList.count > 0){
            return UIEdgeInsetsMake(0, 15, 0, 15);
        }
    }
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    CGFloat spacing = 0.0f;
    if(section == 0){
        spacing = 21;
    }else if (section == 1 || section == 2){
        spacing = 0;
    }else if (section == 3){
        spacing = 15;
    }
    return spacing;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    CGFloat spacing = 0.0f;
    if(section == 0){
        spacing = (int)((collectionView.width - 50*4 - 15*2)/3);
    }else if (section == 1 || section == 2){
        spacing = 0;
    }
    else if (section == 3){
        spacing = 15;
    }
    return spacing;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat width = 0.0f;
    CGFloat height = 0.0f;
    if(indexPath.section == 0){
        width = 50;
        height = 80;
    }else if (indexPath.section == 1 || indexPath.section == 2){
        width = ScreenWidth;
        height = 166;
    }else if (indexPath.section == 3){
        width = (ScreenWidth - 45)/2;
        height = width + 52;
    }
    return CGSizeMake(width, height);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    if(section == 0){
        if(self.hotCategoryList.count > 0){
            return CGSizeMake(ScreenWidth, 70);
        }
    }else if (section == 1){
        if(self.hotGoodsList.count > 0){
            return CGSizeMake(ScreenWidth, 70);
        }
    }
    else if (section == 2){
        if(self.inSeasonGoodsList.count > 0){
            return CGSizeMake(ScreenWidth, 70);
        }
    }else if (section == 3){
        if(self.categoryGoodsList.count > 0){
            return CGSizeMake(ScreenWidth, 70);
        }
    }
    return CGSizeZero;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0)
    {
        EJSGoodsListViewController *goodsListVC = [[EJSGoodsListViewController alloc] initWithNibName:@"EJSGoodsListViewController" bundle:nil];
        goodsListVC.categoryModel = self.hotCategoryList[indexPath.item];
        [self.navigationController pushViewController:goodsListVC animated:YES];
    }else if (indexPath.section == 3){
        EJSGoodsDetailInfoViewController *goodsDetailVC = [[EJSGoodsDetailInfoViewController alloc] initWithNibName:@"EJSGoodsDetailInfoViewController" bundle:nil];
        goodsDetailVC.goodsId = [self.categoryGoodsList[indexPath.item] spuId];
        [self.navigationController pushViewController:goodsDetailVC animated:YES];
    }
}

#pragma mark - Request
- (void)getCategoryAdRequest{
    FlyWeakSelf
    EJSAdRequest *req = [EJSAdRequest new];
    req.type = @"category";
    req.categoryId = self.categoryId;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        weakSelf.categoryAdList = response.data;
    } failHandler:^(BaseResponse *response) {
        
    }];
}

- (void)getHotCategoryList{
    FlyWeakSelf
    EJSGoodsCategoryRequest *req = [EJSGoodsCategoryRequest new];
    req.type = @"children";
    req.parentId = self.categoryId;
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        weakSelf.hotCategoryList = response.data;
        [self.collectionView.mj_footer endRefreshing];
        [self.collectionView.mj_header endRefreshing];
        [weakSelf.collectionView reloadData];
    } failHandler:^(BaseResponse *response) {
        
    }];
}
//label 1新品 2热门
- (void)getCategoryGoods:(NSString *)label{
    FlyWeakSelf
    EJSGoodsListRequest *req = [EJSGoodsListRequest new];
    req.categoryId = self.categoryId;
    req.labelId = label;
    req.page = @(_currentPage).stringValue;
    req.pageLimit = @"50";
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        EJSGoodsListModel *listModel = response.data;
        if(label.integerValue == 1){
            if(_currentPage == 1){
                 weakSelf.inSeasonGoodsList = [NSMutableArray arrayWithArray:listModel.list];
            }else{
                [weakSelf.inSeasonGoodsList addObjectsFromArray:listModel.list];
            }
           
        }else{
            if(_currentPage == 1){
                weakSelf.hotGoodsList = [NSMutableArray arrayWithArray:listModel.list];
            }else{
                [weakSelf.hotGoodsList addObjectsFromArray:listModel.list];
            }
        }
        [weakSelf.collectionView reloadData];
    } failHandler:^(BaseResponse *response) {
        
    }];
}

//分类下的商品
- (void)getCategoryGoods{
    FlyWeakSelf
    EJSGoodsListRequest *req = [EJSGoodsListRequest new];
    req.categoryId = self.categoryId;
    req.page = @(_currentPage).stringValue;
    req.pageLimit = @"20";
    [req asyncRequestWithsuccessHandler:^(BaseResponse *response) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
        EJSGoodsListModel *listModel = response.data;
        if(_currentPage == 1){
            weakSelf.categoryGoodsList = [NSMutableArray arrayWithArray:listModel.list];
        }else{
            [weakSelf.categoryGoodsList addObjectsFromArray:listModel.list];
        }
        [weakSelf.collectionView reloadData];
    } failHandler:^(BaseResponse *response) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
    }];
}



@end
